package regexp;

import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2024/10/01
 * java正则表达式
 */
public class RegexpTest {
    @Test
    public void test() {
        String context = "66杜伟毅66，你要瘦下来";
        String regexp = ".*\\d.*$";
        Pattern pattern = Pattern.compile(regexp);
        //拿到引擎
        Matcher matcher = pattern.matcher(context);
        System.out.println(matcher.matches());
    }

    @Test
    public void startTest() {
        String regex = "cat";
        String content = "cat cat dog dog cat";
        Pattern pattern = Pattern.compile(regex);
        //拿到引擎
        Matcher matcher = pattern.matcher(content);

        while (matcher.find()) {
            System.out.println(matcher.start());
            System.out.println(matcher.end());
        }
    }

    @Test
    public void testMatches() {
        String regex = "itnanls";
        String content1 = "itnanls";
        String content2 = "itnanls is very handsome  !";
        String content3 = "My name is itnanls.";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher1 = pattern.matcher(content1);
        Matcher matcher2 = pattern.matcher(content2);
        Matcher matcher3 = pattern.matcher(content3);

        System.out.println("matches1(): " + matcher1.matches());
        System.out.println("lookingAt1(): " + matcher1.lookingAt());
        System.out.println("matches2(): " + matcher2.matches());
        System.out.println("lookingAt2(): " + matcher2.lookingAt());
        System.out.println("matches3(): " + matcher3.matches());
        System.out.println("lookingAt3(): " + matcher3.lookingAt());
    }

    @Test
    public void AssertTest1() {//预测/先行	（模式在前），要求后面的符合匹配
        String regex = ".+(?=</span>)";
        String context = "<span class=\"read-count\">阅读数：641</span>";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(context);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }

    @Test
    public void AssertTest2() {
        String regex = "(?<=<span class=\"read-count\">阅读数：)\\d+";
        String context = "<span class=\"read-count\">阅读数：641</span>";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(context);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }

    @Test
    public void AssertTest3() {//回顾/后发	（模式在后），要求前面的符合匹配
        String regex = "\\d+(?=</span>)";
        String context = "<span class=\"read-count\">阅读数：641</span>";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(context);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }

    @Test
    public void CatchTest3() {//捕获（分组）
        String regex = "(0\\d{2})-(\\d{8})";
        String context = "020-85653333";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(context);
        while (matcher.find()) {
            System.out.println(matcher.group(0));
            System.out.println(matcher.group(1));
            System.out.println(matcher.group(2));
        }
    }

    @Test
    public void CatchTest4() {//捕获(通过组名捕获)
        String regex = "(?<code>\\d{2})-(?<phone>\\d{8})";
        String context = "020-85653333";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(context);
        while (matcher.find()) {
            System.out.println(matcher.group("code"));
            System.out.println(matcher.group("phone"));
        }
    }

    @Test
    public void refTest4() {//反向引用
        String context = "aabbxxccdddsksdhfhshh";
        String regex = "(\\w)\\1";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(context);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }

    @Test
    public void replaceAllTest(){//反向引用2
        String context = "abc aabc bc xxx mm";
        String regex="(a*)(b)(c)";
        System.out.println(context.replaceAll(regex, "$1"));
    }

    @Test
    public void greedTest(){//贪婪
        String regex = "\\d{3,6}";
        String context ="61762828 176 2991 871";
        System.out.println("文本：" + context);
        System.out.println("贪婪模式："+ regex);
        Pattern pattern =Pattern.compile(regex);
        Matcher matcher = pattern.matcher(context);
        while(matcher.find()){
            System.out.println("匹配结果：" + matcher.group(0));
        }
    }

    @Test
    public void testGreed2(){//贪婪2
        String regex = "\\d{1,2}\\d{3,5}";
        String context ="61762828 176 2991 871";
        System.out.println("文本：" + context);
        System.out.println("贪婪模式："+ regex);
        Pattern pattern =Pattern.compile(regex);
        Matcher matcher = pattern.matcher(context);
        while(matcher.find()){
            System.out.println("匹配结果：" + matcher.group(0));
        }
    }

    @Test
    public void testNotGreed(){//懒惰匹配
        String reg="(\\d{1,2}?)(\\d{3,4})";
        String test="61762828 176 2991 87321";
        System.out.println("文本："+test);
        System.out.println("贪婪模式："+reg);
        Pattern p1 =Pattern.compile(reg);
        Matcher m1 = p1.matcher(test);
        while(m1.find()){
            System.out.println("匹配结果："+m1.group(0));
        }
    }
}
